<?php

namespace app\api\controller\msg;

use app\api\model\UserFoodModel;
use app\api\model\UserReminderMessage;
use app\api\model\UserRemindMessageModel;
use app\common\controller\Api;
use app\api\model\msg\ReminderModel;
use think\Db;
use think\Model;
use think\Request;
/**
 * 温馨提醒消息控制器
 */
class Reminder extends Api{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    public function __construct(Request $request = null)
    {
        $this->Model = new ReminderModel;
        parent::__construct($request);
    }
    /*
     * 获取温馨提示消息列表
     */
    public function messageList()
    {
        $limit = input('post.limit');
        $model2 = new UserReminderMessage();
        //温馨消息
        $userMessage = $model2::where(['user_id' => $this->auth->id])->paginate($limit);

//        $model2::where(['user_id' => $this->auth->id])->update(['is_read'=>1]);

        $this->success('请求成功',$userMessage);

    }

    /*
     * 我的打卡消息列表
     */
    public function dakaMessageList()
    {
        $limit = input('post.limit');
        $model2 = new UserRemindMessageModel();
        //温馨消息
        $userMessage = $model2::where(['user_id' => $this->auth->id,'day'=>date('Y-m-d')])
            ->paginate($limit)
            ->toArray();
        foreach($userMessage['data'] as $key => &$val){
            $userFood = UserFoodModel::get(['user_id' => $val['user_id'], 'type' => $val['type'], 'day' => date('Y-m-d')]);
            $val['is_daka'] = $userFood?1:0; //前端判断，如果已经打卡的直接隐藏
        }

        //$userMessage = $model2::where(['user_id' => $this->auth->id,'is_read'=>0])->paginate($limit);

//        $model2::where(['user_id' => $this->auth->id])->update(['is_read'=>1]);

        $this->success('请求成功',$userMessage);

    }

    /**
     * 获取温馨提醒列表
     * @return bool
     */
    public function getlist()
    {
        $data = $this->Model
            ->where(['user_id'=>$this->auth->id])
            ->select();
        $this->returnMsg(true,$data);
    }
    /**
    *   修改温馨提示
    */
    public function add(){
        $row = input();
        $row['user_id'] =$this->auth->id;
        $time = $row['time'];
        $row['repeattime'] = date("H",$row['time']).":".date("i",$row['time']);
        $res = $this->Model->allowField(true)->save($row);
        $this->returnMsg($res,$this->Model->id);
    }
    /**
     *   修改温馨提示
     */
    public function edit(){
        $row = input();
        $row['repeattime'] = date("H",$row['time']).":".date("i",$row['time']);
        $data = $this->Model->where(['id'=>$row['id']])->find();
        $res = $data->allowField(true)->save($row['row']);
        $this->returnMsg($res,$data->id);
    }
    /**
     *   删除温馨提示
     */
    public function delete(){
        $row = input();
        $row['id'] = explode(',',$row['id']);
        $res = $this->Model->where('id','in',$row['id'])->delete();
        $this->returnMsg($res);
    }
    /**
     *   修改温馨提示状态
     */
    public function editStatus(){
        $row = input();
        $data = $this->Model->where(['id'=>$row['id']])->find();
        $res = $data->allowField(true)->save($row);
        $this->returnMsg($res);
    }

    public function returnMsg($res,$data=''){
        if($res){
            $this->success('请求成功',$data);
        }else{
            $this->error('未知错误');
        }
    }
}
